<?php
namespace Foam\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\EventManager\EventManagerInterface;

class ProduitController extends AbstractActionController
{
    protected $produitTable;
    protected $produitAllergeneTable;
    protected $noteTable;
    protected $ficheTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/foam');
        }, 100);
 
        return $this;
    }
    
    public function getProduitTable()
    {
        if (!$this->produitTable) {
            $sm = $this->getServiceLocator();
            $this->produitTable = $sm->get('Foam\Model\ProduitTable');
        }
        return $this->produitTable;
    }
    
    public function getProduitAllergeneTable()
    {
        if (!$this->produitAllergeneTable) {
            $sm = $this->getServiceLocator();
            $this->produitAllergeneTable = $sm->get('Foam\Model\ProduitAllergeneTable');
        }
        return $this->produitAllergeneTable;
    }
    
    public function getNoteTable()
    {
        if (!$this->noteTable) {
            $sm = $this->getServiceLocator();
            $this->noteTable = $sm->get('Foam\Model\NoteTable');
        }
        return $this->noteTable;
    }
    
    public function getFicheTechniqueTable()
    {
        if (!$this->ficheTable) {
            $sm = $this->getServiceLocator();
            $this->ficheTable = $sm->get('Foam\Model\FicheTechniqueTable');
        }
        return $this->ficheTable;
    }
    
    public function indexAction()
    {
        return new ViewModel(array(
            'produits' => $this->getProduitTable()->getAll(),
        ));
    }
    
    public function addAction()
    {
        return new ViewModel();
    }
    
    public function editAction()
    {
        $id = $this->params('id');
        return new ViewModel(array(
            'produit' => $this->getProduitTable()->getProduit($id),
            'allergenes' => $this->getProduitAllergeneTable()->getProduitAllergene($id),
            'notes' => $this->getNoteTable()->getNote($id),
        ));
    }
    
    public function savenewAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $id_produit = '';
            $libelle = ($request->getPost()->libelle)?$request->getPost()->libelle:'';
            $id_categorie = ($request->getPost()->id_categorie)?$request->getPost()->id_categorie:0;
            $id_famille = ($request->getPost()->id_famille)?$request->getPost()->id_famille:0;
            $id_ssfamille = ($request->getPost()->id_ssfamille)?$request->getPost()->id_ssfamille:0;
            $id_gamme = ($request->getPost()->id_gamme)?$request->getPost()->id_gamme:0;
            $id_ssgamme = ($request->getPost()->id_ssgamme)?$request->getPost()->id_ssgamme:0;
            $code = ($request->getPost()->code)?$request->getPost()->code:'';
            $prix_base = ($request->getPost()->prix_base)?$request->getPost()->prix_base:0;
            $presence_ctv = ($request->getPost()->presence_ctv)?$request->getPost()->presence_ctv:0;
            $presence_louvre = ($request->getPost()->presence_louvre)?$request->getPost()->presence_louvre:0;
            $livraison = ($request->getPost()->livraison)?$request->getPost()->livraison:0;
            $prix_figaro = ($request->getPost()->prix_figaro)?$request->getPost()->prix_figaro:0;
            $food_paper_cost = ($request->getPost()->food_paper_cost)?$request->getPost()->food_paper_cost:0;
            $marge = ($request->getPost()->marge)?$request->getPost()->marge:0;
            $id_production = ($request->getPost()->id_production)?$request->getPost()->id_production:0;
            $target = ($request->getPost()->target)?$request->getPost()->target:0;
            $serie_10 = str_replace(',','.',($request->getPost()->serie_10)?$request->getPost()->serie_10:0);
            $fiche_tek_hermes = str_replace(',','.',($request->getPost()->fiche_tek_hermes)?$request->getPost()->fiche_tek_hermes:0);
            $fiche_tek_formation = str_replace(',','.',($request->getPost()->fiche_tek_formation)?$request->getPost()->fiche_tek_formation:0);
            $certification_bio = ($request->getPost()->certification_bio)?$request->getPost()->certification_bio:'n/a';
            $presence_lactose = ($request->getPost()->presence_lactose)?$request->getPost()->presence_lactose:'n/a';
            $id_contenant = str_replace(',','.',($request->getPost()->id_contenant)?$request->getPost()->id_contenant:0);
            $id_dlc = str_replace(',','.',($request->getPost()->id_dlc)?$request->getPost()->id_dlc:0);
            $id_service = str_replace(',','.',($request->getPost()->id_service)?$request->getPost()->id_service:0);
            $a_savoir = str_replace(',','.',($request->getPost()->a_savoir)?$request->getPost()->a_savoir:'');
            $temps_presence = str_replace(',','.',($request->getPost()->temps_presence)?$request->getPost()->temps_presence:'');
            $ingredients_etiquette = ($request->getPost()->ingredients_etiquette)?$request->getPost()->ingredients_etiquette:'';
            $produit_complementaire = ($request->getPost()->produit_complementaire)?$request->getPost()->produit_complementaire:'';
            $prix_printemps = ($request->getPost()->prix_printemps)?$request->getPost()->prix_printemps:0;
            $tva_sp = ($request->getPost()->tva_sp)?$request->getPost()->tva_sp:0;
            $tva_vae = ($request->getPost()->tva_vae)?$request->getPost()->tva_vae:0;
            $prix_concession = ($request->getPost()->prix_concession)?$request->getPost()->prix_concession:0;
            $volumes_estimes = ($request->getPost()->volumes_estimes)?$request->getPost()->volumes_estimes:0;
            $image = ($request->getPost()->image)?$request->getPost()->image:'';
            $actif = ($request->getPost()->actif)?$request->getPost()->actif:1;
            $date_actif = ($request->getPost()->date_actif)?$request->getPost()->date_actif:'';
            
            $id_note = '';
            $achat = $request->getPost()->achat;
            $labo = $request->getPost()->labo;
            $qualite = $request->getPost()->qualite;
            $marketing = $request->getPost()->marketing;
            $formation = $request->getPost()->formation;
            $operation = $request->getPost()->operation;
            $rd = $request->getPost()->rd;
        
        
            // Insertion du produit dans la table Produit
            $lastIdProduit = $this->getProduitTable()->saveNew($id_produit,$libelle,$id_categorie,$id_famille,$id_ssfamille,$id_gamme,$id_ssgamme,$code,$prix_base,$presence_ctv,$presence_louvre,$livraison,$prix_figaro,$food_paper_cost,$marge,$id_production,$target,$serie_10,$fiche_tek_hermes,$fiche_tek_formation,$certification_bio,$presence_lactose,$id_contenant,$id_dlc,$id_service,$a_savoir,$temps_presence,$ingredients_etiquette,$produit_complementaire,$prix_printemps,$tva_sp,$tva_vae,$prix_concession,$volumes_estimes,$image,$actif,$date_actif);

            // Insertion des allergenes produit dans la table ProduitAllergene
            if(isset($request->getPost()->id_allergene)){
                foreach($request->getPost()->id_allergene AS $id_allergene){
                    $this->getProduitAllergeneTable()->addProduitAllergene('',$id_allergene,$lastIdProduit);
                }
            }

            // Insertion des notes produit dans la table Note
            $this->getNoteTable()->addNote($id_note,$achat,$labo,$qualite,$marketing,$formation,$operation,$rd,$lastIdProduit);

            // Mis à jour des IDs produit dans la table FicheTechnique
            $this->getFicheTechniqueTable()->updateFicheProduitId($lastIdProduit);
        }

        $viewModel = new ViewModel(array(
            'message' => 'Nouveau produit enregistré',
        ));

        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function saveupdateAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $id_produit = $request->getPost()->id_produit;
            $libelle = ($request->getPost()->libelle)?$request->getPost()->libelle:'';
            $id_categorie = ($request->getPost()->id_categorie)?$request->getPost()->id_categorie:0;
            $id_famille = ($request->getPost()->id_famille)?$request->getPost()->id_famille:0;
            $id_ssfamille = ($request->getPost()->id_ssfamille)?$request->getPost()->id_ssfamille:0;
            $id_gamme = ($request->getPost()->id_gamme)?$request->getPost()->id_gamme:0;
            $id_ssgamme = ($request->getPost()->id_ssgamme)?$request->getPost()->id_ssgamme:0;
            $code = ($request->getPost()->code)?$request->getPost()->code:'';
            $prix_base = ($request->getPost()->prix_base)?$request->getPost()->prix_base:0;
            $presence_ctv = ($request->getPost()->presence_ctv)?$request->getPost()->presence_ctv:0;
            $presence_louvre = ($request->getPost()->presence_louvre)?$request->getPost()->presence_louvre:0;
            $livraison = ($request->getPost()->livraison)?$request->getPost()->livraison:0;
            $prix_figaro = ($request->getPost()->prix_figaro)?$request->getPost()->prix_figaro:0;
            $food_paper_cost = ($request->getPost()->food_paper_cost)?$request->getPost()->food_paper_cost:0;
            $marge = ($request->getPost()->marge)?$request->getPost()->marge:0;
            $id_production = ($request->getPost()->id_production)?$request->getPost()->id_production:0;
            $target = ($request->getPost()->target)?$request->getPost()->target:0;
            $serie_10 = str_replace(',','.',($request->getPost()->serie_10)?$request->getPost()->serie_10:0);
            $fiche_tek_hermes = str_replace(',','.',($request->getPost()->fiche_tek_hermes)?$request->getPost()->fiche_tek_hermes:0);
            $fiche_tek_formation = str_replace(',','.',($request->getPost()->fiche_tek_formation)?$request->getPost()->fiche_tek_formation:0);
            $certification_bio = ($request->getPost()->certification_bio)?$request->getPost()->certification_bio:'n/a';
            $presence_lactose = ($request->getPost()->presence_lactose)?$request->getPost()->presence_lactose:'n/a';
            $id_contenant = str_replace(',','.',($request->getPost()->id_contenant)?$request->getPost()->id_contenant:0);
            $id_dlc = str_replace(',','.',($request->getPost()->id_dlc)?$request->getPost()->id_dlc:0);
            $id_service = str_replace(',','.',($request->getPost()->id_service)?$request->getPost()->id_service:0);
            $a_savoir = str_replace(',','.',($request->getPost()->a_savoir)?$request->getPost()->a_savoir:'');
            $temps_presence = str_replace(',','.',($request->getPost()->temps_presence)?$request->getPost()->temps_presence:'');
            $ingredients_etiquette = ($request->getPost()->ingredients_etiquette)?$request->getPost()->ingredients_etiquette:'';
            $produit_complementaire = ($request->getPost()->produit_complementaire)?$request->getPost()->produit_complementaire:'';
            $prix_printemps = ($request->getPost()->prix_printemps)?$request->getPost()->prix_printemps:0;
            $tva_sp = ($request->getPost()->tva_sp)?$request->getPost()->tva_sp:0;
            $tva_vae = ($request->getPost()->tva_vae)?$request->getPost()->tva_vae:0;
            $prix_concession = ($request->getPost()->prix_concession)?$request->getPost()->prix_concession:0;
            $volumes_estimes = ($request->getPost()->volumes_estimes)?$request->getPost()->volumes_estimes:0;
            $image = ($request->getPost()->image)?$request->getPost()->image:'';
            $actif = ($request->getPost()->actif)?$request->getPost()->actif:1;
            $date_actif = ($request->getPost()->date_actif)?$request->getPost()->date_actif:'';
            
            $achat = $request->getPost()->achat;
            $labo = $request->getPost()->labo;
            $qualite = $request->getPost()->qualite;
            $marketing = $request->getPost()->marketing;
            $formation = $request->getPost()->formation;
            $operation = $request->getPost()->operation;
            $rd = $request->getPost()->rd;
        }
        
        // Mis à jour dans la table Produit
        $this->getProduitTable()->saveUpdate($id_produit,$libelle,$id_categorie,$id_famille,$id_ssfamille,$id_gamme,$id_ssgamme,$code,$prix_base,$presence_ctv,$presence_louvre,$livraison,$prix_figaro,$food_paper_cost,$marge,$id_production,$target,$serie_10,$fiche_tek_hermes,$fiche_tek_formation,$certification_bio,$presence_lactose,$id_contenant,$id_dlc,$id_service,$a_savoir,$temps_presence,$ingredients_etiquette,$produit_complementaire,$prix_printemps,$tva_sp,$tva_vae,$prix_concession,$volumes_estimes,$image,$actif,$date_actif);
        
        // Delete product allergene
        $this->getProduitAllergeneTable()->deleteProduitAllergene($id_produit);
        
        // Insert new product allergene
        if(isset($request->getPost()->id_allergene)){            
            foreach($request->getPost()->id_allergene AS $id_allergene){                
                $this->getProduitAllergeneTable()->addProduitAllergene('',$id_allergene,$id_produit);
            }
        }
        
        // Mis à jour des notes produit dans la table Note
        if($this->getNoteTable()->getNote($id_produit))
            $this->getNoteTable()->updateNote($achat,$labo,$qualite,$marketing,$formation,$operation,$rd,$id_produit);
        else 
            $this->getNoteTable()->addNote('',$achat,$labo,$qualite,$marketing,$formation,$operation,$rd,$id_produit);
        
        $viewModel = new ViewModel(array(
            'message' => 'Mofication enregistrée',
        ));

        $viewModel->setTerminal(true);
        //var_dump($request->getPost());
        
        return $viewModel;
    }
    
    public function deleteAction()
    {
        $id = $this->params('id');
        
        // Delete produit
        $this->getProduitTable()->deleteProduit($id);
        
        // Delete product allergene
        $this->getProduitAllergeneTable()->deleteProduitAllergene($id);
        
        // Delete product note
        $this->getNoteTable()->deleteNote($id);
        
        // Delete produit ficheTek
        $this->getFicheTechniqueTable()->deleteByProduit($id);
        
        return $this->redirect()->toRoute('foam/default', array('controller'=>'produit'));
    }
}